//
//  WelcomeViewController.swift
//  WeiBo09
//
//  Created by 裘明 on 15/9/8.
//  Copyright © 2015年 qm. All rights reserved.
//

import UIKit

class WelcomeViewController: UIViewController {

    // 头像底部的约束
    private var iconBottomCons: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        /// 从网络加载头像
        iconView.sd_setImageWithURL(UserAccountViewModel.sharedUserAccount.avatar_large)
        // 设置界面
        setupUI()
    }
    
    override func viewDidAppear(animated: Bool) {
        
        // 计算距顶部的约束值
        let h = -(UIScreen.mainScreen().bounds.height + self.iconBottomCons!.constant)
        
        iconBottomCons?.constant = h
        
        label.alpha = 0
        
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 12, options: [], animations: { () -> Void in
            // 如果需要时自动更新布局
            self.view.layoutIfNeeded()

            }) { (_) -> Void in
                
                UIView.animateWithDuration(1.2, animations: { () -> Void in
                    
                    self.label.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        
                        // 通知appDelegate去却换控制器
                        NSNotificationCenter.defaultCenter().postNotificationName(MQSwitchRootViewController, object: nil)
                })
        }
    }
    /// 设置界面
    private func setupUI() {
        
        // 1.添加控件
        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(label)
        // 2.设置约束
        backImageView.ff_Fill(view)
        
        let cons =  iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 90, height: 90), offset: CGPointMake(0, -200))
        
        self.iconBottomCons = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
        
        label.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPointMake(0, 16))
        
    }
    
    // 1.背景视图
    private lazy var backImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))

    // 2.用户头像
    private lazy var iconView: UIImageView = {
       
        let iv = UIImageView(image: UIImage(named: "avatar_default_big"))
        iv.layer.cornerRadius = 45
        iv.layer.masksToBounds = true
        return iv
    }()
    
    // 3.文本标签
    private lazy var label: UILabel = UILabel(title: "欢迎归来", color: UIColor.darkGrayColor(), fontSize: 18)
    
}
